<?php

!defined('DEBUG') AND exit('Access Denied.');

$action = param(1);

// hook my_start.php

$user = user_read($uid);
user_login_check();

$header['mobile_title'] = $user['username'];
$header['mobile_linke'] = url("my");

is_numeric($action) AND $action = '';

$active = $action;

// hook my_action_before.php

if (empty($action)) {

	$header['title'] = lang('my_home');
	//user_thread_quota_updata_day();
	$group = group_read($user['gid']);
	include _include(APP_PATH . 'view/htm/my.htm');

/*
} elseif($action == 'profile') {

if($ajax) {
// user_safe_info($user);
message(0, $user);
} else {
include _include(APP_PATH.'view/htm/my_profile.htm');
}
 */

} elseif ($action == 'password') {

	if ($method == 'GET') {

		// hook my_password_get_start.php

		include _include(APP_PATH . 'view/htm/my_password.htm');

	} elseif ($method == 'POST') {

		// hook my_password_post_start.php

		$password_old = param('password_old');
		$password_new = param('password_new');
		$password_new_repeat = param('password_new_repeat');
		$password_new_repeat != $password_new AND message(-1, lang('repeat_password_incorrect'));
		md5($password_old . $user['salt']) != $user['password'] AND message('password_old', lang('old_password_incorrect'));
		$password_new = md5($password_new . $user['salt']);
		$r = user_update($uid, array('password' => $password_new));
		$r === FALSE AND message(-1, lang('password_modify_failed'));

		// hook my_password_post_end.php
		message(0, lang('password_modify_successfully'));

	}

} elseif ($action == 'thread') {

	// hook my_thread_start.php

	$page = param(2, 1);
	$pagesize = 20;
	$totalnum = $user['threads'];

	// hook my_profile_thread_list_before.php

	$pagination = pagination(url('my-thread-{page}'), $totalnum, $page, $pagesize);
	$threadlist = mythread_find_by_uid($uid, $page, $pagesize);

	// hook my_thread_end.php

	include _include(APP_PATH . 'view/htm/my_thread.htm');

} elseif ($action == 'avatar') {

	if ($method == 'GET') {

		// hook my_avatar_get_start.php

		include _include(APP_PATH . 'view/htm/my_avatar.htm');

	} else {

		// hook my_avatar_post_start.php

		$width = param('width');
		$height = param('height');
		$data = param('data', '', FALSE);

		empty($data) AND message(-1, lang('data_is_empty'));
		$data = base64_decode_file_data($data);
		$size = strlen($data);
		$size > 40000 AND message(-1, lang('filesize_too_large', array('maxsize' => '40K', 'size' => $size)));

		$filename = "$uid.png";
		$dir = substr(sprintf("%09d", $uid), 0, 3) . '/';
		$path = $conf['upload_path'] . 'avatar/' . $dir;
		$url = $conf['upload_url'] . 'avatar/' . $dir . $filename;
		!is_dir($path) AND (mkdir($path, 0777, TRUE) OR message(-2, lang('directory_create_failed')));

		// hook my_avatar_post_save_before.php
		file_put_contents($path . $filename, $data) OR message(-1, lang('write_to_file_failed'));

		user_update($uid, array('avatar' => $time));

		// hook my_avatar_post_end.php

		message(0, array('url' => $url));

	}
} elseif ($action == 'rmbs') {
	if ($method == 'GET') {
		include _include(APP_PATH . 'view/htm/my_rmbs.htm');
	} else {
		# code...
	}
} elseif ($action == 'rmbsadd') {
	if ($method == 'GET') {
		include _include(APP_PATH . 'view/htm/my_rmbs_add.htm');
	}
	# code...
} elseif ($action == 'verified') {
	if ($method == 'GET') {
		$verified_check = verified_if_uid($uid);
		include _include(APP_PATH . 'view/htm/my_verified.htm');
	} elseif ($method == 'POST') {
		$verified_check = verified_if_uid($uid);
		if ($verified_check == -2) {
			# code...

			$name = param('name');
			$number = param('cardid');
			$passwd = param('password');

			$cpurl = param('cpurl');
			$cnurl = param('cnurl');
			$bcurl = param('bcurl');

			empty($name) AND message('name', '没有姓名');
			empty($number) AND message('cardid', '没有身份证号');
			empty($cpurl) AND message('vcard_cp', '没有上传身份证正面');
			empty($cnurl) AND message('vcard_cn', '没有上传身份证反面');
			empty($bcurl) AND message('vcard_b', '没有上传名片');
			empty($passwd) AND message('password', '没有密码');

			verified_create($uid, $name, $number, $cpurl, $cnurl, $bcurl);

			message(0, '提交成功');
			//verified_create($uid,);
		} else {
			message(0, '非法操作');
		}
	}
} elseif ($action == 'upload_verified') {
	$action = param(2);
	$width = param('width');
	$height = param('height');
	$data = param('data', '', FALSE);

	empty($data) AND message(-1, lang('data_is_empty'));
	$data = base64_decode_file_data($data);
	$size = strlen($data);
	$size > 2048000 AND message(-1, lang('filesize_too_large', array('maxsize' => '2M', 'size' => $size)));

	$filename = "$action.png";
	$dir = "$uid/";
	$t = time();
	$path = $conf['upload_path'] . 'verified/' . $t . '/' . $dir;
	$url = $conf['upload_url'] . 'verified/' . $t . '/' . $dir . $filename;
	!is_dir($path) AND (mkdir($path, 0777, TRUE) OR message(-2, lang('directory_create_failed')));

	// hook my_avatar_post_save_before.php
	file_put_contents($path . $filename, $data) OR message(-1, lang('write_to_file_failed'));

	//user_update($uid, array('avatar' => $time));

	// hook my_avatar_post_end.php

	$_SESSION["verified_" . $uid . "_$action"] = $url;

	message(0, array('url' => $url));

} elseif ($action == 'shop') {
	$action = param(2);
	if ($action == 'list') {
		$shoplist = shop_read_uid($uid);



		include _include(APP_PATH . 'view/htm/my_shop_list.htm');
	} elseif ($action == 'create') {
		if($method == 'GET'){
			include _include(APP_PATH . 'view/htm/my_shop_create.htm');
		} elseif($method == 'POST'){
			$name = param('name','');
			$logo = param('logo','');
			$intro = param('intro','');

			empty($name) AND message('name', '没有填写名称');
			empty($logo) AND message('logo', '没有选择logo');
			empty($intro) AND message('intro', '没有填写介绍');

			shop_create($uid,$name,$logo,$intro);

			message(0,'提交成功，等待审核');
		}
		
	} elseif ($action == 'admin') {
		if ($method == 'GET') {
			$shop = param(3);
			$_shoplist = shop_read_uid($uid);
			//$shoplist = arrlist_change_key($shoplist,'sid');
			$shoplist = array();
			foreach($_shoplist as $k=>$v) {
				$shop = $v;

				//$shop['logo'] = '';

				$shop['intro'] = htmlspecialchars_decode($v['intro']);
				$shop['service'] = htmlspecialchars_decode($v['service']);
				$shop['hot'] = htmlspecialchars_decode($v['hot']);
				$shop['contact'] = htmlspecialchars_decode($v['contact']);
				$shoplist[$v['sid']] = $shop;
			}
			include _include(APP_PATH . 'view/htm/my_shop_admin.htm');
		} else {
			$sid = param('admin_shop','');
			$name = param('name','');
			$logo = param('logo','');
			$intro = param('intro','');
			$service = param('service','');
			$hot = param('hot','');
			$contact = param('contact','');
			$slide_1 = param('slide_1','');
			$slide_2 = param('slide_2','');
			$slide_3 = param('slide_3','');
			$slide_4 = param('slide_4','');

			$shop = shop_read($sid);

			$_shop = array();

			($name == '' OR $name == $shop['name'] )?:$_shop['name'] = $name;
			($logo == '' OR $logo == $shop['name'] )?:$_shop['logo'] = $logo;
			($service == '' OR $service == $shop['service'] )?:$_shop['service'] = $service;
			($intro == '' OR $intro == $shop['intro'] )?:$_shop['intro'] = $intro;
			($hot == '' OR $hot == $shop['hot'] )?:$_shop['hot'] = $hot;
			($contact == '' OR $contact == $shop['contact'] )?:$_shop['contact'] = $contact;
			($slide_1 == '' OR $slide_1 == $shop['slide_1'] )?:$_shop['slide_1'] = $slide_1;
			($slide_2 == '' OR $slide_2 == $shop['slide_2'] )?:$_shop['slide_2'] = $slide_2;
			($slide_3 == '' OR $slide_3 == $shop['slide_3'] )?:$_shop['slide_3'] = $slide_3;
			($slide_4 == '' OR $slide_4 == $slide_4['name'] )?:$_shop['slide_4'] = $slide_4;
			
			shop_update($sid,$_shop);
			message(0,"修改成功");
		}
		

	} 
} elseif ($action == 'group') {
	$action = param(2);
	if($action == 'up'){
		if ($method == 'GET') {
			$grouplist = group_list_cache();
			$ugroup = group_read($user['gid']);
			$grouplist = arrlist_cond_orderby($grouplist,array('gid' => array('>=' => '100')));
			include _include(APP_PATH . 'view/htm/my_group_up.htm');
		} else {
			
		}
	}
}

// hook my_end.php

?>